#include "MenuSystem.h"
#include "IrControl.h"
#include <fcntl.h>
#include "Menu/Tool/frame.h"


#define ITEM_TEST_COLOR	(0)
#define ITEM_TEST_SOUND	(1)
#define ITEM_TEST_TOUCH	(2)
#define ITEM_TEST_INFO		(3)

#define TEST_MAIN_BUTTON_QTY   (4)
UInt32 LastKey = IRC_TOUCH_UP;

TPosCoor	TestMainItemPos[TEST_MAIN_BUTTON_QTY] = { {50,140}, {400,140}, {50,280},{400,280} };
TRect		TestMainTouchRect[TEST_MAIN_BUTTON_QTY];
int			TestMainFocus = -1;
TFrame			TestMainFrame;
char		*TestMainInfoStr[TEST_MAIN_BUTTON_QTY] = {"COLOR","SOUND","TOUCH","INFO"};
extern unsigned char rece_handle;	
extern unsigned char dtpresent_flag;
extern pthread_cond_t dt_cond;

void (*TestMainBtnEnterDispatch[])() = {TestColorInit,TestSoundInit,TestTouchInit,DispVerInit};



int TestMainGetTouchID( TPosCoor pos )
{
	int i;		
	
		for( i = 0; i < TEST_MAIN_BUTTON_QTY; i++ )
			if( ut_PtInRect( pos, TestMainTouchRect[i] ) )
				{	
				printf ("return is %d \n",i);
				return i;				
				}
	printf ("pos.x is %d,pos.y is %d ,TestMainTouchRect is %d,%d,%d,%d\n",pos.x,pos.y,TestMainTouchRect[0] ,\
		TestMainTouchRect[1] ,TestMainTouchRect[2] ,TestMainTouchRect[3] );	
	return -1;
}

void TestMainDrawItem( TCanvas *pCanvas, int idx, BOOL bfocus )
{
	TJpeg pJpeg;
	TPosCoor pos;
	char FileName[20];
	int GetJpegIdx;

	if( idx < 0 || idx >= TEST_MAIN_BUTTON_QTY )
		return;

	if( bfocus ){
		GetJpegIdx = IMG_TEST_MAIN_ITEM_FOCUS_START+idx ;
	}else{
		GetJpegIdx = IMG_TEST_MAIN_ITEM_COLOR+idx ;
	}
	g_SetTextFormat( FONT33, TEXT_NORMAL, RGB_WHITE, 0, 0 );
	pos = TestMainItemPos[idx];
	sprintf( FileName, "/%d/%d.jpg", TEST_MAIN_FOLDER, GetJpegIdx);	
	pJpeg = GetJpegPtr(FileName);
	DrawJpeg(pCanvas, pos.x, pos.y, FileName);
	TestMainTouchRect[idx].left = pos.x;
	TestMainTouchRect[idx].right = pos.x + pJpeg.Width;
	TestMainTouchRect[idx].top = pos.y;
	TestMainTouchRect[idx].bottom = pos.y + pJpeg.Height;
	g_Puts( pCanvas, FONT33, TestMainTouchRect[idx], TEXT_ALIGN_CENTER, TestMainInfoStr[idx] );
	
}

void TestMainDraw( BOOL bDrawAll )
{
	int i;
	DrawJpeg(TestMainFrame.pCanvas, 0, 0, "/logo.jpg" );
	//g_FillRect( TestMainFrame.pCanvas, 0, 0, SCREEN_WIDTH - 1, SCREEN_HEIGHT - 1, RGB_BLACK );
	for( i = 0; i < TEST_MAIN_BUTTON_QTY; i++ )
		TestMainDrawItem( TestMainFrame.pCanvas, i, FALSE );
		
	//dtpresent_flag++;		
	//pthread_cond_signal(&dt_cond);
	dt_Present();
}




BOOL TestMainBtnEnter(int idx)
{
	if (idx <0)
		return TRUE;
	TestMainBtnEnterDispatch[idx](NULL);	
	LOG( "btn = %d aaa\n", idx );	

	return TRUE;
}

#ifdef ENGINEER_UPGRADE

void TestMainInit( int flag)
{
	char cmd[20];
	TRect rect;
	int x = 0;
	
	
	frm_Create( &TestMainFrame, FRM_NO_TITLE|FRM_NO_TASK, "" );		
	if((flag >= 0) && (flag <= 100))
	{
		if(flag == 0)
		{
			g_DrawRect(TestMainFrame.pCanvas,99,179,501,241,_RGB(0,0,0));
		}
		else
		{
			x = 100 + 4 * (flag-1);
			g_FillRect(TestMainFrame.pCanvas,x,180,(x+4),240,_RGB(128,64,0));
		}

		sprintf(cmd,"%d%%",flag);
		g_SetTextColor(FONT18,_RGB(250,250,250),0);
		//g_FillRect(TestMainFrame.pCanvas,260,245,340,280,_RGB(152,152,152));
		DrawJpeg(TestMainFrame.pCanvas, 260, 245, "/background.jpg" );
		g_PutsXY(TestMainFrame.pCanvas,FONT18,260,245,cmd);

		dt_Present();	
	}
	else
	{
		switch (flag)
		{
			case 1000:
				sleep (1);
				DrawJpeg(TestMainFrame.pCanvas, 0, 0, "/upgrade_start.jpg" );
				dt_Present();
				break;
			case 1001:
	 			//g_FillRect( TestMainFrame.pCanvas, 0, 0, SCREEN_WIDTH - 1, SCREEN_HEIGHT - 1, RGB_BLACK );
				DrawJpeg(TestMainFrame.pCanvas, 0, 0, "/upgrade_end.jpg" );
				dt_Present();	
				break;
			case 1002:
	 			//g_FillRect( TestMainFrame.pCanvas, 0, 0, SCREEN_WIDTH - 1, SCREEN_HEIGHT - 1, RGB_BLACK );
				DrawJpeg(TestMainFrame.pCanvas, 0, 0, "/upgrade_error.jpg" );
				dt_Present();	
				break;	
			case 1003:
				DrawJpeg(TestMainFrame.pCanvas, 0, 0, "/upgrade_ing.jpg" );
				dt_Present();	
			default:
				break;
		}
	}
 
}
#else

void TestMainInit()
{
	
	ir_SetCurrentID( IR_TEST_MAIN);
	frm_Create( &TestMainFrame, FRM_NO_TITLE|FRM_NO_TASK, "" );
	TestMainDraw( TRUE );
	printf ("in test main Init ~~\n");
	
}
#endif




void TestMainInput(UInt32 key)
{	
	if( key == IRC_TOUCH_DOWN || key == IRC_TOUCH_UP )
	{
		int focus;
		TPosCoor pos;
		dt_ClipTouchGetXY( &pos.x, &pos.y );
		printf ("pos.x is %d,pos.x is %d~~~\n",pos.x,pos.y);
		if( key == IRC_TOUCH_DOWN )
		{
			if( LastKey ==IRC_TOUCH_UP )
			{
				if( ( TestMainFocus = TestMainGetTouchID( pos )) != -1 )
				{
					printf ("first change color button!\n");
					TestMainDrawItem( TestMainFrame.pCanvas, TestMainFocus, TRUE );
					dt_Present_part(TestMainTouchRect [TestMainFocus]);
				}
				else
					printf ("TestMainFocus is %d\n");				
			}
		}
		else
		{
		printf ("second release the button!\n");
			if( ( focus = TestMainGetTouchID( pos ) ) == TestMainFocus )
				TestMainBtnEnter( TestMainFocus );
			else if( TestMainFocus != -1 )
			{
				TestMainDrawItem( TestMainFrame.pCanvas, TestMainFocus, FALSE );
				dt_Present_part(TestMainTouchRect [TestMainFocus]);
			}
			TestMainFocus = -1;
		}
		LastKey = key;//hangsheng add 071126
	}
	
}


